package com.msblearn.C_011;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.UUID;

public class T002_TestHashMap {
    static HashMap<UUID, UUID> table = new HashMap<>();
    static int count = Constans.COUNT;
    static UUID[] keys = new UUID[count];
    static UUID[] values = new UUID[count];
    static final int table_count = Constans.TABLE_COUNT;

    static {
        for (int i=0;i<count;i++){
            keys[i] = UUID.randomUUID();
            values[i]  = UUID.randomUUID();
        }
    }

    static class MyThread extends Thread{
        int start;
        int gap = count/table_count;

        public MyThread(int start){
            this.start = start;
        }

        @Override
        public void run() {
            for(int i=start;i<start+gap;i++){
                table.put(keys[i],values[i]);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("线程启动");
        long start = System.currentTimeMillis();

        Thread[] threads = new Thread[table_count];

        for(int i=0; i<threads.length; i++) {
            threads[i] =
                    new MyThread(i * (count/table_count));
        }

        for(Thread t : threads) {
            t.start();
        }

        for(Thread t : threads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        long end = System.currentTimeMillis();
        System.out.println(end - start);

        System.out.println(table.size());
    }
}
